/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <QDate>
#include <QStringList>
#include <QTreeWidgetItem>

#include "reportoptionsdialog.h"

ReportOptionsDialog::ReportOptionsDialog(QWidget *parent) : QDialog(parent)
{
    setupUi(this);

    // This column holds the iso date string used to sort the items.
    monthList->setColumnHidden(0, true);
}

QList<QDate> ReportOptionsDialog::getDates() const
{
    QList<QDate> dates;

    QList<QTreeWidgetItem *> monthsSelected;
    monthsSelected = monthList->selectedItems();

    // Put the selected tags into a list.
    Q_FOREACH (QTreeWidgetItem *month, monthsSelected) {
        dates.append(QDate::fromString(month->text(0), Qt::ISODate));
    }

    return dates;
}

ReportOptions ReportOptionsDialog::getOptions() const
{
    ReportOptions options;

    options.listingMonth = listingMonthCheck->isChecked();
    options.listingYear = listingYearCheck->isChecked();
    options.chartTags = chartTagsCheck->isChecked();
    options.chartPayee = chartPayeeCheck->isChecked();
    options.chartPaid = chartPaidCheck->isChecked();
    options.chartAutoPay = chartAutoPayCheck->isChecked();
    options.chartRecurring = chartRecurringCheck->isChecked();
    options.chartTotals = chartTotalsCheck->isChecked();
    options.optionPaid = optionPaidCheck->isChecked();
    options.optionUnpaid = optionUnpaidCheck->isChecked();
    options.optionRecurring = optionRecurringCheck->isChecked();
    options.optionNonRecurring = optionNonRecurringCheck->isChecked();
    options.optionAutoPay = optionAutoPayCheck->isChecked();
    options.optionNonAutoPay = optionNonAutoPayCheck->isChecked();

    return options;
}

void ReportOptionsDialog::setDates(QSet<QString> dates)
{
    Q_FOREACH(QString isoDate, dates) {
        QStringList columns;
        QDate date;

        // We are taking strings in this function and turning them into QDate
        // because QDate cannot be put into a QSet.
        date = QDate::fromString(isoDate, Qt::ISODate);

        columns << isoDate << QDate::longMonthName(date.month())
            << QString::number(date.year());

        QTreeWidgetItem *dateItem = new QTreeWidgetItem(columns);
        monthList->insertTopLevelItem(0, dateItem);
    }

    monthList->sortItems(0, Qt::AscendingOrder);

    if (monthList->topLevelItemCount() > 0) {
        // We don't have to set selected because the multi-select mode
        // will select the item when setting the current item.
        monthList->setCurrentItem(monthList->topLevelItem(0));
    }
}

void ReportOptionsDialog::setOptions(const ReportOptions &options)
{
    listingMonthCheck->setChecked(options.listingMonth);
    listingYearCheck->setChecked(options.listingYear);
    chartTagsCheck->setChecked(options.chartTags);
    chartPayeeCheck->setChecked(options.chartPayee);
    chartPaidCheck->setChecked(options.chartPaid);
    chartAutoPayCheck->setChecked(options.chartAutoPay);
    chartRecurringCheck->setChecked(options.chartRecurring);
    chartTotalsCheck->setChecked(options.chartTotals);
    optionPaidCheck->setChecked(options.optionPaid);
    optionUnpaidCheck->setChecked(options.optionUnpaid);
    optionRecurringCheck->setChecked(options.optionRecurring);
    optionNonRecurringCheck->setChecked(options.optionNonRecurring);
    optionAutoPayCheck->setChecked(options.optionAutoPay);
    optionNonAutoPayCheck->setChecked(options.optionNonAutoPay);
}

void ReportOptionsDialog::on_selectAllButton_clicked()
{
    monthList->selectAll();
}

void ReportOptionsDialog::on_selectNoneButton_clicked()
{
    monthList->clearSelection();
}
